﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BuildDatabase.Repository;
using Ducu.BLL.Entity;
using Entity = Ducu.BLL.Entity;
using Ducu.GLB.Story;
using Global.Core.Helper;
using Ducu.GLB.Global;

namespace BuildDatabase.Factory
{
    internal static class TaskFactory
    {
        internal static void Create()
        {
            ye_voa_7_days();
            luomu_reading_90_days();
            abu_3_tasks_fulfil();
        }

        private static void luomu_reading_90_days()
        {
            for (int i = 0; i < 90; i++)
            {
                DateTime complete = setCompleteDate(BaseDateTime.Point.AddDays(-i));

                if (!PlanRepository.Luomu_Reading.Rest(complete))
                {
                    if (i % 7 != 0)
                    {
                        if (i % 11 == 0)
                        {
                            Task task = new Task 
                            { 
                                CompleteDate = complete,
                                Plan = PlanRepository.Luomu_Reading
                            };

                            NHProvider.session.Save(task);

                            new HistoryItem
                            {
                                Status = Status.Leave,
                                Task = task,
                                Executor = Repository.UserRepository.LuoMu
                            }.Leave();
                        }
                        else
                        {

                            Task task = createOnPlanLuomuReading(complete);

                            if (i % 5 == 0)
                            {
                                new HistoryItem
                                {
                                    Status = Status.Accepted,
                                    Task = task,
                                    Executor = Repository.UserRepository.Abu
                                }.Accept();
                            }
                            else if (i % 4 == 0)
                            {
                                new HistoryItem
                                {
                                    Status = Status.Refused,
                                    Task = task,
                                    Executor = Repository.UserRepository.Abu
                                }.Accept();
                            }
                            else if (i % 3 == 0)
                            {
                                new HistoryItem
                                {
                                    Status = Status.Question,
                                    Task = task,
                                    Executor = Repository.UserRepository.Abu
                                }.Accept();
                            }
                        }
                    }
                }
            }
        }

        private static void ye_voa_7_days()
        {
            Plan plan = PlanRepository.Yezi_VOA;

            int rest = 0;
            int wait_complete = 2;

            for (int i = 0; i < 7; i++)
            {
                DateTime complete = setCompleteDate(BaseDateTime.Point.AddDays(-i));

                if (plan.Rest(complete))
                {
                    rest++;
                }

                //"wait_complete + rest" means those weekdays has been used
                else if (i == (wait_complete + rest))
                {
                    //waiting to accept
                    Entity.Task task_1 = createOnPlanYeziVOA(complete);
                }

                else if (i == (wait_complete + rest) + 1)
                {
                    //refused to accept
                    Entity.Task task_2 = createOnPlanYeziVOA(complete);

                    //only care the accept's but not the plan's create date. 
                    SystemTime.SetDateTime(complete.AddMinutes(245));

                    new HistoryItem
                    {
                        Status = Status.Refused,
                        MyComment = "这肯定不行！",
                        Task = task_2,
                        Executor = Repository.UserRepository.Abu
                    }.Accept();

                    SystemTime.ResetDateTime();
                }

                else if (i == (wait_complete + rest) + 2)
                {

                    //accepted 
                    Entity.Task task_3 = createOnPlanYeziVOA(complete);

                    SystemTime.SetDateTime(complete.AddMinutes(1052));

                    new HistoryItem
                    {
                        Status = Status.Accepted,
                        Task = task_3,
                        Executor = Repository.UserRepository.LuoMu
                    }.Accept();

                    SystemTime.ResetDateTime();

                }

                else if (i == (wait_complete + rest) + 3)
                {
                    //question 
                    Entity.Task task_3 = createOnPlanYeziVOA(complete);

                    SystemTime.SetDateTime(complete.AddMinutes(105));

                    new HistoryItem
                    {
                        Status = Status.Question,
                        MyComment = "没看到证据啊？",
                        Task = task_3,
                        Executor = Repository.UserRepository.LuoMu
                    }.Accept();

                    SystemTime.ResetDateTime();

                }
            }
        }

        private static void abu_3_tasks_fulfil()
        {
            SystemTime.SetDateTime(BaseDateTime.Point.AddDays(-3));
            Entity.Task task_3 = createOnPlanAbuAccount();
            buildTask(task_3);

            SystemTime.SetDateTime(BaseDateTime.Point.AddDays(-2));
            Entity.Task task_2 = createOnPlanAbuAccount();
            buildTask(task_2);

            SystemTime.SetDateTime(SystemTime.Now().AddHours(2));
            new HistoryItem
            {
                Executor = UserRepository.Yezi,
                MyComment = "不行！",
                Status = Status.Refused,
                Task = task_2
            }.Accept();

            SystemTime.SetDateTime(SystemTime.Now().AddHours(3).AddMinutes(21));
            HistoryItem reply_2 = new HistoryItem
            {
                Executor = UserRepository.Abu,
                MyComment = "证据已经重新上传",
                Status = Status.Resume,
                Task = task_2
            };
            reply_2.Reply();


            SystemTime.SetDateTime(BaseDateTime.Point.AddDays(-1));
            Entity.Task task_1 = createOnPlanAbuAccount();
            buildTask(task_1);

            SystemTime.ResetDateTime();
        }

        private static void buildTask(Task task)
        {
            buildTask(task, string.Empty);
        }

        private static void buildTask(Task task, string comment)
        {
            HistoryItem history = new HistoryItem
            {
                MyComment = comment,
                Status = Status.WaitAccept,
                Task = task,
                Executor = task.Plan.Vower
            };
            history.Build();
        }

        private static Task createOnPlanAbuAccount()
        {
            Entity.Task task = new Entity.Task
            {
                Plan = PlanRepository.Abu_Exam,
                CompleteDate = setCompleteDate(SystemTime.Now())
            };
            NHProvider.session.Save(task);
            return task;
        }

        private static DateTime setCompleteDate(DateTime dateTime)
        {
            return new DateTime(dateTime.Year, dateTime.Month, dateTime.Day);
        }

        private static Task createOnPlanYeziVOA(DateTime complete)
        {
            Entity.Task task = new Entity.Task
            {
                Plan = PlanRepository.Yezi_VOA
            };
            task.CompleteDate = complete;
            buildTask(task);
            NHProvider.session.Save(task);
            return task;
        }

        private static Task createOnPlanLuomuReading(DateTime complete)
        {
            Task task = new Task
            {
                Plan = PlanRepository.Luomu_Reading,
                CompleteDate = complete
            };
            buildTask(task);
            NHProvider.session.Save(task);
            return task;
        }
    }
}
